//
//  DaysHistogramViewCell.m
//  MonitoringNetwork
//
//  Created by admin on 2018/1/4.
//  Copyright © 2018年 wtmcxlm. All rights reserved.
//

#import "DaysHistogramViewCell.h"
#import "DaysHistogramView.h"

@interface DaysHistogramViewCell()<UIScrollViewDelegate>

@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *totalLabel;
@property (nonatomic,strong)UIScrollView *baseScrollView;
@property (nonatomic,strong)UILabel *firstVerticallabel;
@property (nonatomic,strong)DaysHistogramView *daysHistogramView;

@end
@implementation DaysHistogramViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    UIView *backView = [UIView new];
    backView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(11);
    }];
    
    _titleLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:@"行驶里程分布"];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(24);
        make.top.mas_equalTo(self).offset(30);
    }];
    
    _totalLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:right andString:@"总计:   "];
    [self addSubview:_totalLabel];
    [_totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).offset(-24);
        make.top.mas_equalTo(self).offset(30);
    }];
    


}
- (void)laodUIWithDataArr:(NSArray *)arr
{
    if (arr.count == 0) {
        UILabel *centerLabel = [UILabel LabelWithFont:BigFontSize andColor:TitleColor andTextAlignment:center andString:@"无统计数据"];
        [self addSubview:centerLabel];
        [centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        
        return;
    }
    
    
    int maxNum = 0;
    int totalNum = 0;
    for (int i=0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        totalNum += [dic[@"value"] intValue];
        if (maxNum<[dic[@"value"] intValue]) {
            maxNum = [dic[@"value"] intValue];
        }
    }
    if (!(totalNum>0)) {
        UILabel *centerLabel = [UILabel LabelWithFont:BigFontSize andColor:TitleColor andTextAlignment:center andString:@"无统计数据"];
        [self addSubview:centerLabel];
        [centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        return;
    }
    
    int HorizontalSpacing = (kScreenWidth - 173.0f/3)/(arr.count-0.5);
    if (arr.count == 1) {
        HorizontalSpacing = 60;
    }else{
        
        if (HorizontalSpacing >60) {
            
        }else{
            HorizontalSpacing = 60;
        }
    }
    
    //竖向线
    UIBezierPath *linePath1 = [UIBezierPath bezierPath];
    [linePath1 moveToPoint:CGPointMake(173.0f/3, 188.0f/3)];
    [linePath1 addLineToPoint:CGPointMake(173.0f/3, 637/3.0f)];
    CAShapeLayer *lineLayer1 = [CAShapeLayer layer];
    lineLayer1.lineWidth = 0.5f;
    lineLayer1.strokeColor = [UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f].CGColor;
    lineLayer1.path = linePath1.CGPath;
    lineLayer1.fillColor = nil;
    [self.layer addSublayer:lineLayer1];
    
    
   
    int eachIntValu = maxNum/(4.f);
    NSString *str  = [NSString stringWithFormat:@"%d",eachIntValu];
    int eachValu =  ceil(maxNum*1.0f/(4.f)/pow(10,str.length-1))*pow(10,str.length-1);
    
    CGFloat lineVerticalLength = 261-143/3.0f - 188.0f/3;
    
    CGFloat verticalLength = lineVerticalLength/(4+1.f);
    
    CGFloat x = 173.0f/3 ;
    CGFloat y = 261-143/3.0f;
    
    
    _totalLabel.text = [NSString stringWithFormat:@"总计 %d",totalNum];
    //竖向label
    for (int i=0; i<5; i++) {
        UILabel *verticallabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:right andString:[NSString stringWithFormat:@"%d",eachValu*i]];
        [self addSubview:verticallabel];
        [verticallabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_left).offset(x-6);
            make.bottom.mas_equalTo(self.mas_top).offset(y-verticalLength*(i));
        }];
    }
    
    //创建滑动视图
    _baseScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(173.0f/3, 188.0f/3, kScreenWidth - 173.0f/3 ,548.f/3)];
    [self addSubview:_baseScrollView];
    _baseScrollView.delegate = self;
    _baseScrollView.contentSize = CGSizeMake(HorizontalSpacing*arr.count+60, 548.f/3);
    _baseScrollView.bounces = NO;
    _baseScrollView.showsHorizontalScrollIndicator = NO;
    //创建背景
    UIView *canv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, arr.count*HorizontalSpacing+60,548.f/3)];
    [_baseScrollView addSubview:canv];
    
    //横向线
    UIBezierPath *linePath2 = [UIBezierPath bezierPath];
    [linePath2 moveToPoint:CGPointMake(0, 0)];
    [linePath2 addLineToPoint:CGPointMake(0, 637/3.0f-188.0f/3)];
    [linePath2 addLineToPoint:CGPointMake((arr.count)*HorizontalSpacing, 637/3.0f-188.0f/3)];
    CAShapeLayer *lineLayer2 = [CAShapeLayer layer];
    lineLayer2.lineWidth = 0.5f;
    lineLayer2.strokeColor = [UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f].CGColor;
    lineLayer2.path = linePath2.CGPath;
    lineLayer2.fillColor = nil;
    [canv.layer addSublayer:lineLayer2];
    
    for (int i=0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        UILabel *verticallabel = [UILabel LabelWithFont:MinFontSize andColor:TitleColor andTextAlignment:right andString:[NSString stringWithFormat:@"%@",dic[@"key"]]];
        [canv addSubview:verticallabel];
        [verticallabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(canv.mas_left).offset(HorizontalSpacing*i+30);
            make.bottom.mas_equalTo(canv.mas_bottom);
        }];
    }
    
    _daysHistogramView = [DaysHistogramView new];
    [canv addSubview:_daysHistogramView];
    [_daysHistogramView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(canv);
        make.bottom.mas_equalTo(canv.mas_top).offset(637/3.0f-188.0f/3+1);
    }];
    [_daysHistogramView loadDaysHistogramWithArr:arr];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat W = scrollView.contentOffset.x;
    if (W) {
        _firstVerticallabel.hidden = YES;
    }else{
        _firstVerticallabel.hidden = NO;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _titleLabel.text = _title;
}
- (void)drawRect:(CGRect)rect
{
    
}
@end
